package com.hk.webapp.controller.admin;

import com.hk.webapp.annotations.SkipPermission;
import com.hk.webapp.bean.CmsCategory;
import com.hk.webapp.bean.SysUserArticleCate;
import com.hk.webapp.service.CmsCategoryService;
import com.hk.webapp.service.SysUserArticleCateService;
import com.hk.webapp.vo.EditCmsCateVo;
import com.hk.webapp.vo.ResultVo;
import com.hk.webapp.vo.UpdateActionVo;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@RestController
public class CategoryController extends AdminBaseController {

    @Resource
    private CmsCategoryService cmsCategoryService;
    @Resource
    private SysUserArticleCateService sysUserArticleCateService;

    @SkipPermission
    @PostMapping("/category/GetAll")
    public ResultVo getAll(){
        List<CmsCategory> allAction = cmsCategoryService.getEntityList();
        return ResultVo.createBySuccessData(allAction.parallelStream().map(c->{
            HashMap<String, Object> map= Maps.newHashMap();
            map.put("id",c.getId());
            map.put("name",c.getName());
            map.put("img_url",c.getImgUrl());
            map.put("imgs_url",c.getImgsUrl());
            map.put("parent_id",c.getParentId());
            map.put("weight",c.getWeight());
            map.put("create_time",c.getCreateTime());
            map.put("custom_property",c.getCustomProperty());
            map.put("class_name",c.getClassName());
            map.put("code",c.getCode());
            map.put("status",c.getStatus());
            map.put("rel_cids",c.getRelCids());
            return map;
        }));
    }

    @SkipPermission
    @PostMapping("/category/GetAllByUser")
    public ResultVo GetAllByUser(@RequestHeader(value = "userId") Integer userId){
        String ids=null;
        if (userId!=1){
            SysUserArticleCate sysUserArticleCate=sysUserArticleCateService.selectBySysUserId(userId);
            if (sysUserArticleCate!=null&&!sysUserArticleCate.getArticleIds().equals("")){
                ids=sysUserArticleCate.getArticleIds();
            }else{
                ids="0";
            }
        }
        List<CmsCategory> allAction = cmsCategoryService.getEntityListByIds(ids);
        return ResultVo.createBySuccessData(allAction.parallelStream().map(c->{
            HashMap<String, Object> map= Maps.newHashMap();
            map.put("id",c.getId());
            map.put("name",c.getName());
            map.put("img_url",c.getImgUrl());
            map.put("imgs_url",c.getImgsUrl());
            map.put("parent_id",c.getParentId());
            map.put("weight",c.getWeight());
            map.put("create_time",c.getCreateTime());
            map.put("custom_property",c.getCustomProperty());
            map.put("class_name",c.getClassName());
            map.put("code",c.getCode());
            map.put("status",c.getStatus());
            map.put("rel_cids",c.getRelCids());
            return map;
        }));
    }

    @PostMapping("/Category/UpdateCate")
    public ResultVo updateCate(@RequestBody UpdateActionVo vo){
        if (vo.getId()==0|| StringUtils.isBlank(vo.getField())){
            return ResultVo.createByError("参数错误");
        }
        if(cmsCategoryService.updateFieldValue(vo)>0){
            return  ResultVo.createBySuccessData("succeed");
        }
        return ResultVo.createByError("编辑失败");
    }

    @PostMapping("/Category/GetInfo")
    public ResultVo getInfo(Integer id){
        if (id<=0){
            return ResultVo.createByError("非法参数");
        }
        CmsCategory data=cmsCategoryService.getEntityById(id);
        if (data==null){
            return ResultVo.createByError("数据不存在或已删除");
        }
        return ResultVo.createBySuccessData(data);
    }

    @PostMapping("/Category/EditCate")
    public ResultVo editCate(@RequestBody @Valid EditCmsCateVo vo){
        if (vo.getId()<=0){
            return ResultVo.createByError("非法参数");
        }
        vo.setCode(StringUtils.isBlank(vo.getCode()) ? null : vo.getCode());

        CmsCategory cmsCategory=new CmsCategory();
        cmsCategory.setId(vo.getId());
        cmsCategory.setParentId(vo.getParent_id());
        cmsCategory.setName(vo.getName());
        cmsCategory.setWeight(vo.getWeight());
        cmsCategory.setCustomProperty(vo.getCustom_property());
        cmsCategory.setClassName(vo.getClass_name());
        cmsCategory.setCode(vo.getCode());
        cmsCategory.setImgUrl(vo.getImg_url());
        cmsCategory.setImgsUrl(vo.getImgs_url());
        cmsCategory.setBak(vo.getBak());
        cmsCategory.setBak2(vo.getBak2());
        cmsCategory.setStatus(vo.getStatus());

        if(cmsCategoryService.update(cmsCategory)>0){
            return ResultVo.createBySuccessData("succeed");
        }
        return ResultVo.createByError("修改失败");

    }

    @PostMapping("/Category/AddCate")
    public ResultVo addCate(@RequestBody @Valid EditCmsCateVo vo){
        vo.setCode(StringUtils.isBlank(vo.getCode()) ? null : vo.getCode());

        CmsCategory cmsCategory=new CmsCategory();
        cmsCategory.setParentId(vo.getParent_id());
        cmsCategory.setName(vo.getName());
        cmsCategory.setWeight(vo.getWeight());
        cmsCategory.setCustomProperty(vo.getCustom_property());
        cmsCategory.setClassName(vo.getClass_name());
        cmsCategory.setCode(vo.getCode());
        cmsCategory.setImgUrl(vo.getImg_url());
        cmsCategory.setImgsUrl(vo.getImgs_url());
        cmsCategory.setBak(vo.getBak());
        cmsCategory.setBak2(vo.getBak2());
        cmsCategory.setStatus(vo.getStatus());

        if(cmsCategoryService.insert(cmsCategory)>0){
            return ResultVo.createBySuccessData("succeed");
        }
        return ResultVo.createByError("添加失败");
    }

    @PostMapping("/Category/DelCate")
    public ResultVo delCate(@RequestBody List<Integer> ids){
        if (ids.size() == 0)
        {
            return ResultVo.createByError("参数错误");
        }
        if (cmsCategoryService.softDelByIds(ids)>0){
            return ResultVo.createBySuccessData("succeed");
        }
        return  ResultVo.createByError("删除失败");
    }
}
